<template>
  <div>
    <u-upload
      :auto-upload="false"
      @on-choose-complete="onChooseComplete"
      :show-progress="false"
      ref="uUpload"
    ></u-upload>
  </div>
</template>

<script>
import uploadFileToCDN from "./upload";
// 组件只支持图片，选择图片上传也是已第一帧作为图片上传
export default {
  data() {
    return {
      fileList: [],
      // 保存一份图片cdn数据，用于其他错误 ，不需要重新上传，直接获取
      cdnfileList: [],
    };
  },
  mounted() {},
  methods: {
    onChooseComplete(files) {
      this.fileList = files;
    },
    // 上传图片
    uploadPic(callBack) {
      // 如果存在cdn数据，用Promise包裹cdnfileList返回保持数据一致
      const promises =
        this.cdnfileList.length === 0
          ? Promise.all(this.fileList.map((file) => uploadFileToCDN(file.url)))
          : Promise.resolve(this.cdnfileList);
        
      promises.then((list) => (this.cdnfileList = list));
      callBack(promises);
    },
    // 重置上传图片
    resetFileList(){
       this.$refs["uUpload"].clear()
       this.onChooseComplete([])
       this.cdnfileList=[]
    }

  }

};
</script>

<style></style>
